#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_new_AE import *
#from EA import *

class dialog_new_EA(QtGui.QDialog, Ui_Dialog_new_AE):
    def __init__(self,parent,AE_parent, relation_type_id,libmyCF):
        super(dialog_new_EA,self).__init__(parent)
        
        self.setupUi(self)
        self.parent=parent
        self.AE_parent = AE_parent
        self.relation_type_id = relation_type_id
        self.libmyCF = libmyCF
        
        # Set parent and relation pre-selected
        self.lineEdit_parent.setText( QtCore.QString.fromUtf8(self.AE_parent.name))
        self.lineEdit_relation.setText( QtCore.QString.fromUtf8(self.libmyCF.ontology.all_relations_type[self.relation_type_id].name))
        
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
    
    def accept(self):
        self.name = unicode(self.lineEdit_Latin_name.text()).encode('utf-8')
        self.name = self.name.capitalize()
        self.lineEdit_Latin_name.setText(QtCore.QString(self.name))
        self.definition = unicode(self.textEdit_Definition.document().toPlainText()).encode('utf-8')
        self.source = unicode(self.lineEdit_source.text()).encode('utf-8') 
        self.curator = unicode(self.lineEdit_curator.text()).encode('utf-8') 
        if len(self.name)>1:
            already_exist = False
            for AE in self.libmyCF.ontology.all_AEs :
                if str(self.libmyCF.ontology.all_AEs[AE].name) == self.name :
                    already_exist = True
            if already_exist :
                QtGui.QMessageBox.warning(self,"myCF warning","An other AE with the same name already exists.\nPlease enter a new name")
            else: 
                self.newAE = self.libmyCF.ontology.add_AE( self.AE_parent.AE_id, self.relation_type_id, self.name, self.definition, self.source, self.curator)
                #self.AE_id = self.newAE.AE_id
                #self.relation_id = newAE.relation_id
                QtGui.QDialog.accept(self)
